<?php

class Ext_View_Result
{
    protected $_view;
    
    public function __construct()
    {
        $front = Zend_Controller_Front::getInstance();
        $bootstrap = $front->getParam('bootstrap');
        $this->_view = $bootstrap->getResource('view');
        
        $this->_view->messages = array();
        $this->_view->result   = 0;
        
        $layout = Zend_Layout::getMvcInstance();
        $request = $front->getRequest();
        
        if ($request->isXmlHttpRequest ()) {
            $layout->setLayout("layout_result-json");
        }else{
            $layout->setLayout("layout_result-html");
            
            $httpHost = $_SERVER['HTTP_HOST'];
            if(isset($_SERVER['HTTP_REFERER'])){
                $referer = $_SERVER['HTTP_REFERER'];
                $_referer = preg_replace("/[a-zA-Z]+:\/\//si", "://", $referer);
                $sub_len = strlen($httpHost);
                $fromServer = substr($_referer, 3, $sub_len);
                if($fromServer == $httpHost){
                    if ($front->getRequest()->isPost()) {
                        $referer = "javascript:history.go(-1);";
                    }
                    
                    $translator = Zend_Registry::get('Zend_Translate');
                    
                    $this->_view->directUrl   = $referer;
                    $this->_view->directTitle = $translator->translate('Click here to go to next page');
                }
            }
        }
    }
    
    public function setLayout($layout = null)
    {
        $zendLayout = Zend_Layout::getMvcInstance();
        if($layout && $layout != null){
            $zendLayout->setLayout($layout);
        }
    }
    
    public function setDirectUrl($url)
    {
        $this->_view->directUrl = $url;
    }
    
    public function setDirectTitle($title)
    {
        $this->_view->directTitle = $title;
    }
    
    public function setResult($result)
    {
        $this->_view->result = $result;
    }
    
    public function getResult()
    {
        return $this->_view->result;
    }
        
    public function addMessage($message)
    {
        $this->_view->messages[] = $message;
    }
    
    public function setMessage($message)
    {
        $this->_view->messages = array();
        
        $this->_view->messages[] = $message;
    }
    
    public function addMessages($messages)
    {
        if(is_array($messages))
        {
            foreach($messages as $key=>$message)
            {
                $this->addMessages($message);
            }
        }
        else 
        {
            $this->addMessage($messages);
        }
    }
    
    public function setMessages($messages)
    {
        $this->_view->messages = array();
        $this->addMessages($messages);
        
        /*
        $front = Zend_Controller_Front::getInstance();
        $response = $front->getResponse();
          $response->insert('messages', $this->_controller->view->render('script_messages.phtml'));
        */
    }
    
    public function setData($data)
    {
        $this->_view->data = $data;
    }
    
    public function setNoRender()
    {
        //$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');
        //$bootstrap->bootstrap('view');
        //$view = $bootstrap->getResource('view');
        Zend_Controller_Front::getInstance()->setParam('noViewRenderer', true);
        //$view->_helper->viewRenderer->setNoRender();
        
        $response = Zend_Controller_Front::getInstance()->getResponse();
        $response->clearBody();
    }
    
    public function setNoLayout()
    {
        $layout = Zend_Layout::getMvcInstance();
        $layout->disableLayout();
    }
    
    public function setRender($render)
    {
        $front = Zend_Controller_Front::getInstance();
        //$this->_controller->view->render($render);
        //$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
        $front->render($render);
    }
    
    public function __get($name)
    {
        $method = 'get'.ucfirst($name);
        if (method_exists($this, $method)) {
            return call_user_func(array(&$this, $method));
        } else {
            require_once 'Zend/View/Exception.php';
            throw new Ext_View_Exception('Property ' . $name . '  does not exist');
        }
    }

    public function __set($name, $val)
    {
        $method = 'set'.ucfirst($name);
        if (method_exists($this, $method)) {
            return call_user_func(array(&$this, $method), $val);
        } else {
            require_once 'Zend/View/Exception.php';
            throw new Ext_View_Exception('Property ' . $name . '  does not exist');
        }
    }
    
    public function __call($method, $params)
    {
        $method = "set" . ucfirst($method);
        if (!method_exists($this, $method)) {
            require_once 'Zend/View/Exception.php';
            throw new Ext_View_Exception(
                'Invalid method "' . $method . '"'
            );
        }

        return call_user_func_array(array($this, $method), $params);
    }
}
